package com.bupt.hotel.hvac.controller;

import com.bupt.hotel.hvac.model.vo.BillVO;
import com.bupt.hotel.hvac.model.vo.DetailRecordVO;
import com.bupt.hotel.hvac.service.BillingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账单服务控制器 - 统一接口标准
 * 提供账单查询、详单查询等功能
 */
@Slf4j
@RestController
@RequestMapping("/api/billing")
public class BillingController {

    private final BillingService billingService;

    public BillingController(BillingService billingService) {
        this.billingService = billingService;
    }

    /**
     * 获取指定房间的账单
     * GET /api/billing/{roomId}
     */
    @GetMapping("/{roomId}")
    public Map<String, Object> getBilling(@PathVariable String roomId) {
        log.info("查询账单 - 房间: {}", roomId);
        
        try {
            BillVO bill = billingService.getBill(roomId);
            
            if (bill == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("error", "账单不存在");
                return result;
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("roomId", bill.getRoomId());
            result.put("totalCost", bill.getTotalCost());
            result.put("totalPowerConsumption", bill.getTotalPowerConsumption());
            result.put("checkInTime", bill.getCheckInTime());
            result.put("checkOutTime", bill.getCheckOutTime());
            
            return result;
        } catch (Exception e) {
            log.error("查询账单失败: {}", e.getMessage());
            Map<String, Object> result = new HashMap<>();
            result.put("error", e.getMessage());
            return result;
        }
    }

    /**
     * 打印账单
     * GET /api/billing/{roomId}/bill
     */
    @GetMapping("/{roomId}/bill")
    public Map<String, Object> printBill(@PathVariable String roomId) {
        log.info("打印账单 - 房间: {}", roomId);
        
        try {
            BillVO bill = billingService.getBill(roomId);
            
            if (bill == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("error", "账单不存在");
                return result;
            }
            
            // 返回完整的账单信息
            Map<String, Object> result = new HashMap<>();
            result.put("roomId", bill.getRoomId());
            result.put("totalCost", bill.getTotalCost());
            result.put("totalPowerConsumption", bill.getTotalPowerConsumption());
            result.put("checkInTime", bill.getCheckInTime());
            result.put("checkOutTime", bill.getCheckOutTime());
            result.put("totalServiceDuration", bill.getTotalServiceDuration());
            
            return result;
        } catch (Exception e) {
            log.error("打印账单失败: {}", e.getMessage());
            Map<String, Object> result = new HashMap<>();
            result.put("error", e.getMessage());
            return result;
        }
    }

    /**
     * 打印详单
     * GET /api/billing/{roomId}/details
     */
    @GetMapping("/{roomId}/details")
    public Map<String, Object> printDetails(@PathVariable String roomId) {
        log.info("打印详单 - 房间: {}", roomId);
        
        try {
            BillVO bill = billingService.getBill(roomId);
            
            if (bill == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("error", "账单不存在");
                return result;
            }
            
            List<DetailRecordVO> details = bill.getDetailRecords();
            
            Map<String, Object> result = new HashMap<>();
            result.put("roomId", bill.getRoomId());
            result.put("details", details);
            result.put("totalRecords", details != null ? details.size() : 0);
            
            return result;
        } catch (Exception e) {
            log.error("打印详单失败: {}", e.getMessage());
            Map<String, Object> result = new HashMap<>();
            result.put("error", e.getMessage());
            return result;
        }
    }
}
